<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    .short_table_tbody button{line-height: 30px!important;height: 30px!important;}
    .short_table_tbody .buttonnone{margin: 5px 20px;color: #707070;background-color: #fff;border: 1px solid #e6e6e6;}
    .short_table_tbody .buttonlv{margin: 5px 20px;background-color: #248BFE}
    .chengObj{display: none}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span id="flowsetname">编辑单方造价表</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>劳务费</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>名称</th>
                            <th><text>*</text>成本合价</th>
                            <th>金额(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list1" style="max-height: 300px"></tbody>
                    </table>
                    <div class="listerror listerror1">提示：<span></span></div>
                </div>
            </div>
        </div>
        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="4">
                    <div class="opt-btn-group">
                        <div><span>材料费</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>名称</th>
                            <th><text>*</text>成本合价</th>
                            <th>金额(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list4" style="max-height: 300px"></tbody>
                    </table>
                    <div class="listerror listerror4">提示：<span></span></div>
                </div>
            </div>
        </div>
        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="3">
                    <div class="opt-btn-group">
                        <div><span>机械费</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>名称</th>
                            <th><text>*</text>成本合价</th>
                            <th>金额(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list3" style="max-height: 300px"></tbody>
                    </table>
                    <div class="listerror listerror3">提示：<span></span></div>
                </div>
            </div>
        </div>
        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="2">
                    <div class="opt-btn-group">
                        <div><span>分包费</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>名称</th>
                            <th><text>*</text>成本合价</th>
                            <th>金额(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list2" style="max-height: 300px"></tbody>
                    </table>
                    <div class="listerror listerror2">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_enginid = getQueryVariable('enginid') || 0;

    function set_cheng(obj) {
        var type = $(obj).parents('.short_table_list').attr('key');
        var key = $(obj).parents('tr').index();
        openPage('huizao_set_cheng.php?key='+key+'&type='+type,'90%','90%');
    }


    var basallist, basalarr;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_main_huizao_edit,data:{enginid:param_enginid}}).done(function(res){
            if (res.code === 2000) {
                basallist = res.data.basallist;
                basalarr = res.data.basalarr;

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td>';
                listAddStr += '<button type="button" onclick="set_cheng(this)" class="layui-btn buttonnone chanbut">';
                listAddStr += '<span>设置</span>';
                listAddStr += '<span class="chengObj"></span>';
                listAddStr += '</button>';
                listAddStr += '</td>';
                listAddStr += '<td class="money"></td>';
                listAddStr += '<td><input class="layui-input explain"></td>';
                short_table_opt_func(listAddStr,1);
                short_table_opt_func(listAddStr,2);
                short_table_opt_func(listAddStr,3);
                short_table_opt_func(listAddStr,4);




                show_listdata(res.data.listtype1, 1);
                show_listdata(res.data.listtype2, 2);
                show_listdata(res.data.listtype3, 3);
                show_listdata(res.data.listtype4, 4);

                render_from();
            }
        });


        form.on('submit(add)', function(data){

            var list = [''];
            try {
                $('#list_list1 tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var cheng = $(this).find('.chengObj').text();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('劳务费：第'+index+'行，请输入名称');
                    }
                    if(!cheng){
                        throw new Error('劳务费：第'+index+'行，请设置成本合价');
                    }


                    list.push({
                        type: 1,
                        name:name,
                        cheng:cheng,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror1").find('span').text(e.message);
                $(".listerror1").show();
                return false;
            }


            try {
                $('#list_list2 tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var cheng = $(this).find('.chengObj').text();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('分包费：第'+index+'行，请输入名称');
                    }
                    if(!cheng){
                        throw new Error('分包费：第'+index+'行，请设置成本合价');
                    }


                    list.push({
                        type: 2,
                        name:name,
                        cheng:cheng,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror2").find('span').text(e.message);
                $(".listerror2").show();
                return false;
            }


            try {
                $('#list_list3 tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var cheng = $(this).find('.chengObj').text();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('机械费：第'+index+'行，请输入名称');
                    }
                    if(!cheng){
                        throw new Error('机械费：第'+index+'行，请设置成本合价');
                    }


                    list.push({
                        type: 3,
                        name:name,
                        cheng:cheng,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror3").find('span').text(e.message);
                $(".listerror3").show();
                return false;
            }


            try {
                $('#list_list4 tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var cheng = $(this).find('.chengObj').text();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('材料费：第'+index+'行，请输入名称');
                    }
                    if(!cheng){
                        throw new Error('材料费：第'+index+'行，请设置成本合价');
                    }


                    list.push({
                        type: 4,
                        name:name,
                        cheng:cheng,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror4").find('span').text(e.message);
                $(".listerror4").show();
                return false;
            }

            $(".listerror").hide();
            data.field.list = list;

            data.field.enginid = param_enginid;
            facade.ajax({url: ask_main_huizao_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });
            return false;
        });

    });




    function show_listdata(data, type) {
        var str = '';
        for (var key in data) {
            var nums = parseInt(key)+1;
            str += '<tr>';
            str += '<td style="width: 45px">'+nums+'</td>';
            str += '<td><input type="text" class="layui-input name" value="'+data[key].name+'"></td>';
            str += '<td>';

            if(data[key].cheng){
                str += '<button type="button" onclick="set_cheng(this)" class="layui-btn buttonlv chanbut">';
            }else{
                str += '<button type="button" onclick="set_cheng(this)" class="layui-btn buttonnone chanbut">';
            }
            str += '<span>设置</span>';
            str += '<span class="chengObj">'+data[key].cheng+'</span>';
            str += '</button>';
            str += '</td>';
            str += '<td class="money">'+data[key].money+'</td>';
            str += '<td><input class="layui-input explain" value="'+data[key].explain+'"></td>';
        }
        if(str){
            $("#list_list"+type).html(str);
        }
    }

</script>
</body>
</html>
